package com.cc.service.generate;


import cn.hutool.core.collection.CollectionUtil;
import com.cc.build.BuilderDaoImpl;
import com.cc.config.PackagePathConfiguration;
import com.cc.config.TemplateConfiguration;
import com.cc.pojo.WhereParams;
import com.cc.pojo.file.BuiderAbstract;
import com.cc.pojo.file.FilePathAndClassName;
import com.cc.pojo.tablestruct.LibaryProperties;
import com.cc.pojo.tablestruct.NeedQueryParams;
import com.cc.pojo.tablestruct.TableStruct;
import com.cc.service.core.GenerateAbstract;
import com.cc.utils.ConvertHumpUnderlinesToEachOtherUtil;
import com.cc.utils.CoreUtil;
import com.cc.utils.beanUtil.BeanUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author c.c
 * @Description 核心生成类
 * @since 2023/3/30
 */

public class GenerateDaoImpl extends GenerateAbstract {
  private PackagePathConfiguration packagePathConfiguration = BeanUtils.getBean(PackagePathConfiguration.class);
  private TemplateConfiguration templateConfiguration = BeanUtils.getBean(TemplateConfiguration.class);

  public GenerateDaoImpl(List<NeedQueryParams> needQueryParams, List<WhereParams> whereParamTemp) {
    this.getTargetPackageAddress(needQueryParams, whereParamTemp);
  }

  public GenerateDaoImpl(HashMap<String, List<NeedQueryParams>> needQueryParams, HashMap<String, List<WhereParams>> whereParamTemp) {
    this.getTargetPackageAddress(needQueryParams, whereParamTemp);
  }

  @Override
  public List<FilePathAndClassName<BuiderAbstract>> getTargetPackageAddress(Object... youParameter) {
    HashMap<String, List<NeedQueryParams>> needQuerymap = null;
    HashMap<String, List<WhereParams>> wheremap = null;
    for (int i = 0; i < youParameter.length; i++) {
      needQuerymap = (HashMap<String, List<NeedQueryParams>>) youParameter[0];
      wheremap = (HashMap<String, List<WhereParams>>) youParameter[1];
    }

    List<FilePathAndClassName<BuiderAbstract>> packageAddress = new ArrayList<>();
    List<LibaryProperties> libaryProperties = super.getLibaryProperties();
    for (LibaryProperties libaryProperty : libaryProperties) {
      if (CollectionUtil.isNotEmpty(libaryProperty.getTableStructs())) {
        StringBuilder packgePath = new StringBuilder();
        CoreUtil.getPath(libaryProperty, packgePath, packagePathConfiguration.isDaoimplisLastLayer(), packagePathConfiguration.getDaoimplPath());
        String classForImpl = ConvertHumpUnderlinesToEachOtherUtil.underlineToHump(libaryProperty.getTableName(), true) + "DaoImpl";
        String classForDo = ConvertHumpUnderlinesToEachOtherUtil.underlineToHump(libaryProperty.getTableName(), true) + "Do";
        String classForDao = ConvertHumpUnderlinesToEachOtherUtil.underlineToHump(libaryProperty.getTableName(), true) + "Dao";
        String classForDoPath = super.getPathAddress(classForDo);
        String classForDaoPath = super.getPathAddress(classForDao);

        //获取表信息
        List<TableStruct> tableStructs = libaryProperty.getTableStructs();
        StringBuilder select = new StringBuilder("SELECT ");
        tableStructs.forEach(tableStruct -> {
          select.append(tableStruct.getColumnName());
          if (tableStructs.indexOf(tableStruct) != tableStructs.size() - 1) {
            select.append(",");
          }
        });
        String from = "FROM " + libaryProperty.getTableName();

        List<NeedQueryParams> needQueryParams = null;
        List<WhereParams> whereParamTemp = null;
        if (needQuerymap != null && wheremap != null) {
          needQueryParams = needQuerymap.get(libaryProperty.getTableName());
          whereParamTemp = wheremap.get(libaryProperty.getTableName());
        }
        BuilderDaoImpl builderDao = new BuilderDaoImpl(packgePath.toString(),
          Arrays.asList(classForDoPath, classForDaoPath),
          classForDo,
          classForDao,
          classForImpl,
          needQueryParams,
          select.toString(),
          from,
          whereParamTemp
        );
        FilePathAndClassName<BuiderAbstract> filePathAndClassName = new FilePathAndClassName<>(packgePath.toString(), classForImpl, builderDao);
        super.putPathAddress(classForImpl, packgePath);
        packageAddress.add(filePathAndClassName);
      }
    }
    super.build(packageAddress, templateConfiguration.getDaoImplPath());
    return packageAddress;
  }
}
